#!./perl

# ------------------------------------------------------------

# initdev.pl, by Jean-Pierre Girard (jpg@corrado.sun.com)
# This allows the development environment to choose what
# page to load depending on the existence of .cdev

# ------------------------------------------------------------

# Bugs and other fixes

# ------------------------------------------------------------

$BASEDIR = $ENV{"DOCUMENT_ROOT"};
require 'devinfo.pl';
require 'tools.pl';
$ShortDir = &GetDevinfo("dir");
$ShortDir =~ s?.*/([^/]*)$?\1?;

if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
} elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
	$request = $ENV{'QUERY_STRING'};
}
$request =~ s/%(..)/pack("c",hex($1))/ge;

if (@ARGV == 0 && $request eq "") {
	if ($ShortDir eq "") {
		$HasDir = 0;
	} else {
		$HasDir = 1;
		$CompDir = "$BASEDIR/$ShortDir";
	}
} elsif ($request eq "DISPLAY") {
	$HasDir = 0;
} else {
	$HasDir = 1;
	
	$DirCompName = ($request eq ""? $ARGV[0]: $request);
	$DirCompName =~ s/^Menu_dir=//;
	$DirCompName =~ s/^IsIndex=//;
	$DirCompName =~ s/[ 	\n]*$//;
	substr($DirCompName, 0, 1) =~ tr/a-z/A-Z/;
	$DirCompName =~ tr/a-zA-Z0-9./_/c;
	$CompDir=$BASEDIR . "/" . $DirCompName;
	if ( -d $CompDir ) {
		unless ( -w $CompDir) {
			$HasDir = 0;
			$Err = 1;
		}
	} else {
		unless (mkdir($CompDir, 0777)) {
			$HasDir = 0;
			$Err = 2;
		}
	}
}

if ($HasDir == 0) {
	$ListDir = `ls $BASEDIR`;
	$ListDir =~ s/hotjava\n|CDware\n|tmp\n|DEV\n|([^ 	\n]*\.Z)//g;
	$ListDir =~ s/(.*)\n+/<OPTION>\1\n/g;
	write;
} else {
	&PrintDevinfo($CompDir, 0);
	$URL="file://" . $BASEDIR . "/DEV/index.html";
	$URL="http://localhost:5759/cgi-bin/mainmenu.pl?";
	print "Location:  $URL\n\n";
	#print "Content-type: text/html\n\n\n<html><body>URL=$URL<br>";
	#print "dir=$CompDir<br>";
	#print "dir devinfo=", &GetDevinfo("dir"), "<br>";
	#print "status devinfo=", &GetDevinfo("status");
	#print "\n</body></html>";
}

format STDOUT =
Content-type: text/html


<html>
<title>Welcome to the Developer's Kit</title>

<body>
<center><IMG src="http://localhost:5759/CDware/images/startup.gif"></center>
@*
($Err == 1 ? "<H3><dd><applet class=\"ImageLoopItem\" img=\"http://localhost:5759/DEV/images/blinkingAlert\" ALIGN=middle pause=0 width=56 height=49 pause = 200>Not a writable directory. Change user or rights.</H3></dd>\n": "")
@*
($Err == 2 ? "<H3><dd><applet class=\"ImageLoopItem\" img=\"http://localhost:5759/DEV/images/blinkingAlert\" ALIGN=middle pause=0 width=56 height=49 pause = 200>Cannot create the directory. Change user or rights of the .products directory.</H3></dd>\n": "")
<center>
<applet class="DancingText" font=TimesRoman30b text="Welcome to the CDware Developer's Kit" time=2000 pause=1500></center>
#<H1>Welcome to the CDware Developer's Kit</H1></center>
<b><p>This kit will help you develop your insertion to the Catalyst program.</br>
It uses HotJava, the web browser developped by SunSoft around Java, to guide you through the different steps of the creation.</p></b>
<p><IMG ALIGN=middle SRC="http://localhost:5759/DEV/images/finger.gif">
The first time you 're starting this Kit, you need to provide a directory name.
<p>This directory will be created in the <b>"@*
$BASEDIR
"</b> directory if it doesn't already exist. Make sure you have write permissions in this directory. This name should resemble your company name, and all the files of your submission will have to be under this directory.
<br><p>Directory name:
<FORM method="post"  ACTION="http://localhost:5759/cgi-bin/initdev.pl">
<INPUT TYPE="text" 
~@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Dir
 NAME="IsIndex" SIZE="35" MAXLENGTH=50
value="@*
$ShortDir
"><input type="submit" VALUE="Ok" >
#<input type="Reset" VALUE="Reset" >
</FORM>
<hr>
<h4>Existing company directories: </h4>
<form method="post"  ACTION="http://localhost:5759/cgi-bin/initdev.pl">
<center><SELECT name="Menu_dir" size="10">
@*
$ListDir
</SELECT>
<input type="submit" VALUE="Ok" >
#<input type="Reset" VALUE="Reset" >
</center></form>
</body></html>
.
